<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------
namespace api\partner\controller;

use api\partner\model\PartnerModel;
use api\partner\model\ShopApplyOrderModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\db\Query;
use think\facade\Cache;
use think\facade\Request;
use api\partner\validate\OrderValidate;

class OrderController extends RestBaseController
{
    protected $token = '';

    public function initialize()
    {
        $token = $this->request->header('XX-Token');

        $id = $this->request->param('partner_id',0,'intval');
        if(!$id){
            $this->error('请求失败');
        }
        $token_cache = Cache::get('partner:'.$id);
        if(!$token || $token!=$token_cache){
            $this->error('请重新登录');
        }else{
            $this->token = $token_cache;
        }
    }
    /**
     * 商户分类
     */
    public function shopCategory()
    {
        $param = $this->request->param();
        
        $category = Db::name('tenant_category')->field('id,parent_id,name,alias,list_order')->where('status',1)->order('list_order asc')->select();
        if(!empty($param['parent_id'])){
            $childCategory = Db::name('tenant_category')->field('id,parent_id,name,alias,list_order')->where(['status'=>1,'parent_id'=>$param['parent_id']])->order('list_order asc')->select();

            $this->success('请求成功',$childCategory);
            
        }else{
            $this->success('请求成功',$category);
        }
    }
    
    /**
     * 提交店铺入驻申请
     */
    public function addShop()
    {
        $param = $this->request->param();
        if(empty($param['license_id'])){
            $param['license_id']='';
        }
        $partner_id = $param['partner_id'];
        $is_save = $param['is_save'];
        if($is_save == 1){
            $validate = new OrderValidate();
            $result = $validate->scene('saveShop')->check($param);
            if(!$result){
                $this->error($validate->getError());
            }
        }else{
            $validate = new OrderValidate();
            $result = $validate->scene('addShop')->check($param);
            if(!$result){
                $this->error($validate->getError());
            }
        }
        
        $order = Db::name('shop_info')->where('license_id',$param['license_id'])->find();
        
        if($order){

            if($order['checked'] == 0){
                $this->error('该企业正在审核中，请耐心等待！');
            }
            //如果先保存过，再次点击提交 为更新
            
            $param['checked'] = $is_save == 1?-1:0;
            unset($param['partner_id']);
            unset($param['id']);
            unset($param['is_save']);

            // $this->success('提交成功，等待平台审核！',$param);

            $res = Db::name('shop_info')->where('id',intval($order['id']))->update($param);
            if($res){
                
                $msg = $is_save==0?'提交成功，等待平台审核！':'保存成功！';
                $this->success('保存成功！');

            }else{
                $this->error('未做修改！');

            }
        }else{
            if($order  && $order['cheched'] == 0){
                $this->error('该企业正在审核中，请耐心等待！');
            }
    
            //判断是否为临时保存
            if($param['is_save'] == 1){
                $param['checked'] = -1;
            }
            unset($param['is_save']);
    
            $param['create_time'] = time();
            unset($param['partner_id']);
    
            //生成店铺唯一ID
            $param['shop_id'] = uniqid('forTenant').time();
    
            $res = Db::name('shop_info')->insertGetId($param);
            
            if($res){
                $addOrder = Db::name('shop_apply_order')->insertGetId(['partner_id'=>$partner_id,'shop_info_id'=>$res]);
                if($addOrder){
                    $msg = $is_save==0?'提交成功，等待平台审核！':'保存成功！';
                    $this->success($msg);
    
                }else{
                    $this->error('订单未添加！');
        
                }
            }else{
                $this->error('提交失败！');
    
            }
        }
        
    }

    /**
     * 修改店铺入驻申请
     */
    public function editShop()
    {
        $param = $this->request->param();
        $partner_id = $param['partner_id'];
        $id = $param['id'];

        $validate = new OrderValidate();
        $result = $validate->scene('editShop')->check($param);
        if(!$result){
            $this->error($validate->getError());
        }
        $order = Db::name('shop_info')
            ->alias('s')
            ->leftJoin('for_shop_apply_order o',['o.shop_info_id = s.id'])
            ->where('s.id',$param['id'])
            ->where('o.partner_id',$partner_id)
            ->find();

        if($order['checked']==0){
            $this->error('该企业正在审核中，请耐心等待！');
        }
        if($order['checked']==1){
            $this->error('该企业已有其他入驻申请！');
        }
        $param['checked'] = 0;
        unset($param['partner_id']);
        unset($param['id']);
        // $this->success('提交成功，等待平台审核！',$param);

        $res = Db::name('shop_info')->where('id',intval($id))->update($param);
        if($res){
            
            $this->success('提交成功，等待平台审核！');

        }else{
            $this->error('提交失败！');

        }
    }

    /**
     * 店铺列表
     * @param page 分页
     * @param category_id 分类
     * @param status 审核状态
     * @param partner_id 业务员ID
     */
    public function lists()
    {
        $page = $this->request->param('page',1,'intval');
        $category_id = $this->request->param('category_id',0,'intval');
        $status = $this->request->param('status');
        $partner_id  = $this->request->param('partner_id',0,'intval');
        if(!$partner_id){
            $this->error('请求异常');
        }
        $orderModel = new ShopApplyOrderModel();
        $list = $orderModel->getList($page-1,0,$status,$category_id,'');

        $this->success('请求成功',$list);
    }
    /**
     * 我的-店铺列表
     * @param page 分页
     * @param category_id 分类
     * @param status 审核状态
     * @param partner_id 业务员ID
     * @param location 前端定位坐标
     */
    public function myLists()
    {
        $page = $this->request->param('page',1,'intval');
        $category_id = $this->request->param('category_id',0,'intval');
        $status = $this->request->param('status',0,'intval');
        $partner_id  = $this->request->param('partner_id',0,'intval');
        $location  = $this->request->param('location');

        if(!$partner_id){
            $this->error('请求异常');
        }
        $orderModel = new ShopApplyOrderModel();
        $list = $orderModel->getList($page-1,$partner_id,$status,$category_id,$location);

        $this->success('请求成功',$list);
    }

    /**
     * 店铺详情
     * @param order_id 订单号
     * @param partner_id 业务员ID
     */
    public function getOrder()
    {
        $partner_id  = $this->request->param('partner_id',0,'intval');
        $order_id  = $this->request->param('order_id',0,'intval');

        $orderModel = new ShopApplyOrderModel();

        $order = $orderModel->getOne($partner_id,$order_id);
        if($order){
            $this->success('请求成功',$order);
        }else{
            $this->error('订单不存在');
        }
    }

     /**
     * 商户服务列表
     */
    public function services()
    {
        $res =  Db::name('tenant_service')->where(['status'=>1])->select();
        
        $this->success('请求成功',$res);
        
    }
}